package com.situ.ftbserver.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.ftbserver.model.Coach;
import com.situ.ftbserver.model.Member;
import com.situ.ftbserver.model.search.CoachSearchBean;
import com.situ.ftbserver.service.CoachService;
import com.situ.ftbserver.service.UploadService;
import com.situ.ftbserver.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/v1/coach",produces = MediaType.APPLICATION_JSON_VALUE)
public class CoachApi {
    private CoachService coachService;
    private UploadService uploadService;
    @Autowired
    public void setCoachService(CoachService coachService) {
        this.coachService = coachService;
    }

    @Autowired
    public void setUploadService(UploadService uploadService) {
        this.uploadService = uploadService;
    }

    @GetMapping
    public ResponseEntity<JsonResult<?>> findAll(@RequestParam(defaultValue ="1")  Integer pageNo,
                                                 @RequestParam(defaultValue ="10") Integer pageSize,
                                                 CoachSearchBean csb){
        Page<Coach> page = Page.of(pageNo, pageSize);

        page = this.coachService.findAll(page, csb);
        return ResponseEntity.ok(JsonResult.success(page));
    }
    @GetMapping("/findCoach")
    public ResponseEntity<JsonResult<?>> findCoach(){

         List<Coach> cs= this.coachService.findCoach();
        return ResponseEntity.ok(JsonResult.success(cs));
    }
    @GetMapping("/getPopularCoach")
    public ResponseEntity<JsonResult<?>> getPopularCoach(){
        List<Map<String,Integer>> cs= coachService.getPopularCoach();
        return ResponseEntity.ok(JsonResult.success(cs));
    }
    @DeleteMapping
    public ResponseEntity<JsonResult<?>> delete(@RequestBody Integer[] ids){
//        System.out.println(ids);
        int delete = coachService.delete(ids);
        if(delete==0){
            return ResponseEntity.ok(JsonResult.fail("删除失败,所选教练有课程"));
        }
        return ResponseEntity.ok(JsonResult.success(delete));
    }
    @PostMapping
    public ResponseEntity<JsonResult<?>> save(@RequestBody Coach coach){
        LocalDate birthday = coach.getBirthday();
        coach.setAge(LocalDate.now().getYear() - birthday.getYear());
//        System.out.println(member.getAge());
        boolean result = this.coachService.save(coach);
        if(result){
            return ResponseEntity.ok(JsonResult.success(coach));
        }else{
            return ResponseEntity.ok(JsonResult.fail("保存失败"));
        }
    }
    //上传会员头像
    @PostMapping("/avatar")
    public ResponseEntity<JsonResult<?>> uploadCoachAvatar(MultipartFile file) {
        String path = this.uploadService.uploadImage(file, "coach_avatar");
        return ResponseEntity.ok(JsonResult.success(path));
    }
    @PutMapping
    public ResponseEntity<JsonResult<?>> update(@RequestBody Coach coach){
        LocalDate birthday = coach.getBirthday();
        coach.setAge(LocalDate.now().getYear() - birthday.getYear());
        boolean result = this.coachService.updateById(coach);
        if(result){
            return ResponseEntity.ok(JsonResult.success(coach));
        }else{
            return ResponseEntity.ok(JsonResult.fail("更新失败"));
        }
    }
}
